package com.py.gs.tools;

import com.py.gs.businese.unionbattletest.KRandom;

/**
 * 从min到(max-1)中产生不重复的随机数，范围比较大的情况不适用
 * 
 * @author cyd
 * @date 2018年4月20日
 */
public class NumberRandom {

	KRandom random;
	int[] array;
	int min;
	int max;
	int pos;
	
	public NumberRandom(int min, int max) {
		int len = max - min;
		if (max < min) {
			throw new RuntimeException("error");
		}
		array = new int[len];
		for (int i = 0; i < array.length; i++) {
			array[i] = i + min;
		}
		this.min = min;
		this.max = max;
		this.random = new KRandom();
	}
	
	public NumberRandom(int min, int max, KRandom random) {
		int len = max - min;
		if (max < min) {
			throw new RuntimeException("error");
		}
		array = new int[len];
		for (int i = 0; i < array.length; i++) {
			array[i] = i + min;
		}
		this.min = min;
		this.max = max;
		this.random = random;
	}
	
	public void reset() {
		pos = 0;
	}
	
	public int nextInt() {
		int index = random.nextInt(array.length-pos) + pos;
		int result = array[index];
		array[index] = array[pos];
		array[pos] = result;
		pos++;
		if (pos >= array.length) {
			pos = 0;
		}
		return result;
	}
}
